////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017-2018 Qualcomm Technologies, Inc.
// All Rights Reserved.
// Confidential and Proprietary - Qualcomm Technologies, Inc.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// THIS IS AUTO-GENERATED CODE.  PLEASE DO NOT EDIT (File bug reports against tools).
///
/// Auto-generated by: ParameterParser V1.1.4
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file  parameterutils.h
/// @brief Auto-generated Chromatix parameter file
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef PARAMETERUTILS_H
#define PARAMETERUTILS_H

#include "parametertypes.h"

// Reads buffer value in little-endian format
class ParameterUtils
{
public:
    static const INT32 UNLIMITED = -1;
    // Integer value read from little-endian binary
    static UINT64 Read(BYTE* buffer, UINT64* pos, UINT64 size)
    {
        UINT64 result = 0;
        for (UINT64 i = 0; i < size; i++)
        {
            result += (((UINT64)buffer[(*pos)++]) << (i * 8));
        }
        return result;
    }
    static BOOL ReadInt8(INT8* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 1;
        if ((*pos) + SIZE <= length)
        {
            INT8* ptr = (INT8*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadUInt8(UINT8* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 1;
        if ((*pos) + SIZE <= length)
        {
            UINT8* ptr = (UINT8*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadInt16(INT16* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 2;
        if ((*pos) + SIZE <= length)
        {
            INT16* ptr = (INT16*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadUInt16(UINT16* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 2;
        if ((*pos) + SIZE <= length)
        {
            UINT16* ptr = (UINT16*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadInt32(INT32* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 4;
        if ((*pos) + SIZE <= length)
        {
            INT32* ptr = (INT32*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadUInt32(UINT32* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 4;
        if ((*pos) + SIZE <= length)
        {
            UINT32* ptr = (UINT32*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadInt64(INT64* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 8;
        if ((*pos) + SIZE <= length)
        {
            INT64* ptr = (INT64*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadUInt64(UINT64* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 8;
        if ((*pos) + SIZE <= length)
        {
            UINT64* ptr = (UINT64*)&buffer[*pos];
            *value = *ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadBoolean(BOOL* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        const UINT64 SIZE = 4;
        if ((*pos) + SIZE <= length)
        {
            INT32* ptr = (INT32*)&buffer[*pos];
            *value = (BOOL)*ptr;
            *pos  += SIZE;
            return TRUE;
        }
        return FALSE;
    }
    static BOOL ReadFloat(FLOAT* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        UINT32 intVal = 0;
        BOOL   result = ReadUInt32(&intVal, buffer, length, pos);
        if (result)
        {
            *value = *((FLOAT*)&intVal);
        }
        return result;
    }
    static BOOL ReadDouble(DOUBLE* value, BYTE* buffer, UINT64 length, UINT64* pos)
    {
        UINT64 intVal = 0;
        BOOL   result = ReadUInt64(&intVal, buffer, length, pos);
        if (result)
        {
            *value = *((DOUBLE*)&intVal);
        }
        return result;
    }
    static BOOL ReadString(CHAR* value, BYTE* buffer, UINT64 length, UINT64* pos, UINT64 destStrSize, UINT64 maxChars)
    {
        UINT64 chars = 0;
        while (chars < maxChars && chars < destStrSize && (*pos) + chars < length && buffer[(*pos) + chars] != 0)
        {
            chars++;
        }

        if (value != NULL)
        {
            for (UINT64 i = 0; i < chars; i++)
            {
                value[i] = (CHAR)buffer[(*pos) + i];
            }

            value[chars] = '\0';
        }

        (*pos) += maxChars;

        return TRUE;
    }
    static CHAR* ReadString(BYTE* buffer, UINT64 length, UINT64* pos, UINT64 maxChars)
    {
        UINT64 chars = 0;
        while (chars < maxChars && (*pos) + chars < length && buffer[(*pos) + chars] != 0)
        {
            chars++;
        }

        CHAR* result = PARAMETER_NEW CHAR[(UINT32)(chars + 1)];
        if (result != NULL)
        {
            for (UINT64 i = 0; i < chars; i++)
            {
                result[i] = (CHAR)buffer[(*pos) + i];
            }

            result[chars] = '\0';
        }

        (*pos) += maxChars;

        return result;
    }
    static BOOL ReadBuffer(BYTE* value, BYTE* buffer, UINT64 length, UINT64* pos, UINT64 count)
    {
        UINT64 bytesCopied = (count <= length - (*pos)) ? count : length - (*pos);
        for (UINT64 i = 0; i < bytesCopied; i++)
        {
            value[i] = buffer[(*pos)++];
        }

        return bytesCopied == count;
    }
};

#endif

